package dao

import (
	"fmt"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/devploys"
	"yundian/internel/app/models/devrule"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/ploys"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type PricesDao struct {
}

func NewPricesDao() *PricesDao {
	return &PricesDao{}
}

// 获取设备类型的计费规则
func (dao *PricesDao) GetDeviceRuleInfo(deviceType int, limit int) *dingo.ErrorWrap {
	var lst []devrule.DeviceRulesModel
	sql := `select * from device_rule where type_id = ? order by set_meal_num asc limit ?`
	db := global.Orm.Raw(sql, deviceType, limit).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// 获取所有计费规则
func (dao *PricesDao) GetDeviceRuleInfoList() *dingo.ErrorWrap {
	var lst []devrule.DeviceRulesModel
	sql := `select * from device_rule`
	db := global.Orm.Raw(sql).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// 获取所有奖励规则
func (dao *PricesDao) GetDevicePloysInfoList() *dingo.ErrorWrap {
	var lst []devploys.DevicePloysModel
	sql := `select * from device_ploys`
	db := global.Orm.Raw(sql).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// 获取体验价格
func (dao *PricesDao) GetFeelPrice() *dingo.ErrorWrap {
	var lst []*prices.PriceModel
	sql := `SELECT * FROM prices where place_id = 1 and device_type_id = 12 order by priority asc limit 3`
	db := global.Orm.Raw(sql).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// 获取价格详情
func (dao *PricesDao) GetPriceDetail(productId string) *dingo.ErrorWrap {
	m := prices.New()
	sql := `select * from prices where product_id = ? ORDER BY create_at DESC LIMIT 1`
	db := global.Orm.Raw(sql, productId).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

// 获取价格详情
func (dao *PricesDao) GetLimitPriceDetail(productId string) *dingo.ErrorWrap {
	m := prices.New()
	sql := `select * from prices where product_id = ?`
	db := global.Orm.Raw(sql, productId).Find(&m).Limit(1)
	return dingo.ErrorIf(db.Error, m)
}

// 获取商户价格
func (dao *PricesDao) GetPlacePriceList(placeId int) *dingo.ErrorWrap {
	var m []prices.PriceWithDeviceType
	sql := `select p.place_id,p.create_at,p.device_type_id,dt.fee_mode,dt.device_name,
dt.shared_mode,dt.device_model_id from prices p left join device_type dt on p.device_type_id = dt.id
where p.place_id = ? group by p.device_type_id`
	db := global.Orm.Debug().Raw(sql, placeId).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

//func (dao *PricesDao) GetPlacePrice(placeId, deviceTypeId int) *dingo.ErrorWrap {
//	var m []prices.PriceModel
//	sql := `select * from prices where place_id = ? and device_type_id = ? order by create_at desc`
//	db := global.Orm.Raw(sql, placeId, deviceTypeId).Find(&m)
//	return dingo.ErrorIf(db.Error, m)
//}

func (dao *PricesDao) GetPlacePriceByTypeId(placeId, deviceTypeId, limit, status int, sort string) *dingo.ErrorWrap {
	var m []prices.PriceModel
	db := global.Orm.Debug().Table("prices").
		Where("place_id = ?", placeId).
		Where("device_type_id = ?", deviceTypeId)

	if status > 0 {
		db.Where("status = ?", status)
	}

	if sort != "" {
		db.Order(sort)
	}
	db.Limit(limit).Find(&m)
	//sql := `select * from prices where place_id = ? and device_type_id = ? and status = 1 order by ? limit ?`
	//db := global.Orm.Debug().Raw(sql, placeId, deviceTypeId, sort, limit).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

// 供微信端获取设备信息用
func (dao *PricesDao) WxGetPlacePriceByTypeId(placeId, deviceTypeId, limit, status, feeMode int, sort string) *dingo.ErrorWrap {
	var m []prices.PriceModel
	db := global.Orm.Debug().Table("prices").
		Where("place_id = ?", placeId).
		Where("device_type_id = ?", deviceTypeId)

	if status > 0 {
		db.Where("status = ?", status)
	}

	if sort != "" {
		db.Order(sort)
	}

	// 套餐20分钟的不显示
	if feeMode == 2 {
		db.Where("set_meal_num != 1")
	}

	db.Limit(limit).Find(&m)
	//sql := `select * from prices where place_id = ? and device_type_id = ? and status = 1 order by ? limit ?`
	//db := global.Orm.Debug().Raw(sql, placeId, deviceTypeId, sort, limit).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

func (dao *PricesDao) GetPlacePriceByTypeIdPre(placeId, deviceTypeId, limit, status int, sort string) *dingo.ErrorWrap {
	var m []*prices.PriceModel
	db := global.Orm.Debug().Table("prices").
		Where("place_id = ?", placeId).
		Where("device_type_id = ?", deviceTypeId)

	if status > 0 {
		db.Where("status = ?", status)
	}

	if sort != "" {
		db.Order(sort)
	}
	db.Limit(limit).Find(&m)
	//sql := `select * from prices where place_id = ? and device_type_id = ? and status = 1 order by ? limit ?`
	//db := global.Orm.Debug().Raw(sql, placeId, deviceTypeId, sort, limit).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

//func (dao *PricesDao) GetSinglePlacePrice(placeId, deviceTypeId int) *dingo.ErrorWrap {
//	var m []prices.PriceModel
//	sql := `select * from prices where place_id = ? and device_type_id = ? order by create_at desc limit 1`
//	db := global.Orm.Raw(sql, placeId, deviceTypeId).Find(&m)
//	return dingo.ErrorIf(db.Error, m)
//}

func (dao *PricesDao) InsertPlacePrice(m *prices.PriceModel) *dingo.ErrorWrap {
	db := global.Orm.Debug().Table("prices").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *PricesDao) InsertPloys(m *ploys.PloyModel) *dingo.ErrorWrap {
	db := global.Orm.Debug().Table("ploys").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *PricesDao) SavePlacePrice(m *prices.PriceModel) *dingo.ErrorWrap {
	db := global.Orm.Table("prices").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *PricesDao) SavePlaceMultiPrice(lst []*prices.PriceModel) *dingo.ErrorWrap {
	db := global.Orm.Table("prices").Create(&lst)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *PricesDao) GetPlaceNameList(placeName string, baseListReq *request.BaseListReq) *dingo.ErrorWrap {

	var (
		lst    []*placemodel.PlaceModel
		count  int64
		limit  int = baseListReq.PageSize
		offset int = baseListReq.PageSize * (baseListReq.PageNo - 1)
	)
	db := global.Orm.Table("places")

	if placeName != "" {
		db.Where(" place_name like ?", fmt.Sprint("%", placeName, "%"))
	}
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

func (dao *PricesDao) PlacePinDingDevice(strList *[]interface{}, updateVal map[string]interface{}) *dingo.ErrorWrap {
	db := global.Orm.Debug().Table("devices").Where(" state = 1  and device_sn in (?)", *strList...).Updates(&updateVal)
	return dingo.ErrorIf(db.Error, true)
}

func (dao *PricesDao) GetPlaceDevice(deviceNo []string) *dingo.ErrorWrap {
	var m []string
	db := global.Orm.Debug().Table("devices").Select("device_sn").Where(" state = 2  and device_sn in (?)", deviceNo).Find(&m)
	return dingo.ErrorIf(db.Error, m)
}

// 根据产品ID查询价格配置
func (dao *PricesDao) GetProductIdPrices(productId string) *dingo.ErrorWrap {
	lst := prices.New()
	db := global.Orm.Table("prices").Where("product_id=?", productId).First(&lst)
	return dingo.ErrorIf(db.Error, lst)
}
